package com.woniu.adapter;

import com.google.common.collect.Lists;
import com.woniu.inlet.fo.ProductFo;
import com.woniu.outlet.elasticSearch.ProductESDao;
import com.woniu.outlet.mysql.ProductMapper;
import com.woniu.outlet.mysql.po.Product;
import com.woniu.outlet.mysql.po.ProductExample;
import com.woniu.outlet.redis.ProductRedisDao;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @author: my
 * @time: 2021/4/26 14:20
 */
@Slf4j
@Component
public class ProductAdapter {
    private final ProductMapper productMapper;
    private final ProductRedisDao productRedisDao;
    private ProductESDao productESDao;

    public ProductAdapter(ProductMapper productMapper,ProductRedisDao productRedisDao,ProductESDao productESDao){
        this.productMapper = productMapper;
        this.productRedisDao = productRedisDao;
        this.productESDao = productESDao;
    }

    @Transactional
    public Product getById(Integer id){
        Optional<Product> opt = productRedisDao.findById(id);
        try {
            Product product = opt.orElseThrow(RuntimeException::new);
            log.debug("从redis中获取product数据");
            return product;
        }catch (RuntimeException e){
            //从数据库获取数据
            Product product = productMapper.selectByPrimaryKey(id);
            if (product != null) {
                productRedisDao.save(product);
            }
            log.debug("从数据库获取product数据");
            return product;
        }
    }

    /**
     * 添加理财产品
     * @param product
     */
    @Transactional
    public void addProduct(Product product){
        productMapper.insert(product);
    }

    /**
     * 获取所有理财产品
     * @return
     */
    @Transactional
    public List<Product> findProducts(ProductFo product){
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if (product.getName() != null) {
            QueryBuilder query = QueryBuilders.matchQuery("name", product.getName());
            queryBuilder.must(query);
        }
        if (product.getType() != null) {
            QueryBuilder query = QueryBuilders.termQuery("type", product.getType());
            queryBuilder.must(query);
        }
        if (product.getStartTime() != null){
            RangeQueryBuilder expire =
                    QueryBuilders.rangeQuery("expire").gte(product.getStartTime());
            queryBuilder.must(expire);
        }
        if (product.getEndTime()!=null){
            RangeQueryBuilder expire = QueryBuilders.rangeQuery("expire").lte(product.getEndTime());
            queryBuilder.must(expire);
        }
        if (product.getStatus() != null) {
            QueryBuilder query = QueryBuilders.termQuery("status", product.getStatus());
            queryBuilder.must(query);
        }
        Iterable<Product> search = productESDao.search(queryBuilder);
        List<Product> products = Lists.newArrayList(search);
        if (products.size() > 0){
            log.debug("从elasticsearch条件获取产品信息");
            return products;
        }
        List<Product> list = productMapper.selectByExample(new ProductExample());
        productESDao.saveAll(list);
        log.debug("从数据库获取数据存入elasticsearch");
        Iterable<Product> search1 = productESDao.search(queryBuilder);
        List<Product> list1 = Lists.newArrayList(search1);
        return list1;
    }

    /**
     * 修改理财产品
     * @param product
     */
    @Transactional
    public void updateProduct(Product product){
        productESDao.deleteAll();
        productRedisDao.deleteById(product.getId());
        productMapper.updateByPrimaryKey(product);
        productRedisDao.deleteById(product.getId());
    }



}
